﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo;
using ConfExpert.Properties;
using ConfExpert.Utility;

using DevExpress.XtraEditors;
using DevTeam;


using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class StokGirisMusteriden : XForm
    {
        public StokGirisMusteriden()
        {
            InitializeComponent();
        }

        private void StokGirisMusteriden_Load(object sender, EventArgs e)
        {

            gridView1.Init("GetMusteridenGelenKumas", WhoAmI, false);
            gridView1.FormatAsNumber(1, "Qty");
            gridView1.AddSummary("Qty");
            gridView1.SetCaptions("MusteridenGelenKumasId:#");
            gridView1.MakeColumnsReadOnly("Tarih", "GelisTarihi", "GelisPartisiId", "RollId");
            gridView1.FormatAsDate("Tarih", "GelisTarihi");
            gridView1.BestFitColumns();

            DataTable dt = gridView1.DataTable();
            dt.PrimaryKey = new DataColumn[] { gridView1.DataTable().Columns["MusteridenGelenKumasId"] };
            dt.AddAutoIncrementalColumn("MusteridenGelenKumasId");
            dt.TableNewRow += StokGirisMusteriden_TableNewRow;
            bbiKaydet.ItemClick += bbiKaydet_ItemClick;

            if (isKD)
            {
                gridView1.MakeCompulsory("ProductId", "Qty", "BrandId", "OwnerId");
                gridView1.YanYanaGetir("MusteridenGelenKumasId:UserId");
                GridManager gm = GridManager.GetManager(gridView1);
                gm.GridMenu.AddMenu("Giriş+Çıkış yap", GirisCikis);
                gm.GridMenu.AddMenu("Kod sihirbazı", KodSihirbazi_Click, Resources.wizardcontrol);
                //gm.GridMenu.AddMenu("Stok kartı düzenle", StokKartlariniDuzenle);
            }
            else
            {
                gridView1.MakeCompulsory("Qty", "BrandId", "OwnerId");
                gridView1.MakeColumnInvisible("UserId");
            }

            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            barCheckItem1.CheckedChanged += barCheckItem1_CheckedChanged;

        }


        void KodSihirbazi_Click(object sender, EventArgs e)
        {
            gridView1.EndEditing();

            List<POKodSihirbazi.Kod> kodlar = new List<POKodSihirbazi.Kod>();
            foreach (int rowHandle in gridView1.GetSelectedRows())
            {
                DataRow dr = gridView1.GetDataRow(rowHandle);

                int productId = dr["ProductId"].ToInt(-1);
                if (productId == -1)
                {
                    POKodSihirbazi.Kod kod = new POKodSihirbazi.Kod();

                    kod.ProductId = dr["ProductId"].ToInt(-1);
                    kod.MusteriKodu = dr["MusteriKodu"].ToStr();
                    if (dr["BrandId"] != null)
                        if (dr["BrandId"] != DBNull.Value)
                            kod.KCustomerId = AppSession.Lookups.Brand.FindByBrandId(dr["BrandId"].ToInt())["CustomerId"].ToInt();

                    kod.UreticiKodu = dr["UreticiKodu"].ToStr();
                    kod.KVendorId = dr["UreticiId"].ToInt(-1);
                    //kod.KaliteAciklama = dr["TalepTeknikOzellik"].ToStr();
                    kod.POLinesId = dr["MusteridenGelenKumasId"].ToInt(-1);
                    kod.RowHandle = rowHandle;

                    if (kod.MusteriKodu != string.Empty || kod.UreticiKodu != string.Empty)
                        kodlar.Add(kod);
                }
            }

            if (kodlar.Count > 0)
                using (POKodSihirbazi ks = new POKodSihirbazi(kodlar))
                {
                    if (ks.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        foreach (POKodSihirbazi.Kod kod in ks.Kodlar)
                            if (kod.ProductId > 0)
                                gridView1.SetRowCellValue(kod.RowHandle, "ProductId", kod.ProductId);

                        //btnKaydet.PerformClick();

                        //GetDetails("PO", "GetPO2", POId);
                    }
                }
        }

        void StokKartlariniDuzenle(object sender, EventArgs e)
        {
            List<string> kodlar = new List<string>();
            foreach (int rowHandle in gridView1.GetSelectedRows())
            {
                DataRow dr = gridView1.GetDataRow(rowHandle);

                if (dr["ProductId"] != DBNull.Value)
                    kodlar.Add(dr["ProductId"].ToString());
            }

            if (kodlar.Count > 0)
                using (StokKartiDuzenle ks = new StokKartiDuzenle(string.Join(";", kodlar.ToArray())))
                {
                    ks.ShowDialog();
                    gridView1.RefreshData();
                }
            else
                XtraMessageBox.Show("Seçtikleriniz içinde, artikeli setlenmiş satır yok.");
        }

        void barCheckItem1_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.RefreshDataSource("GetMusteridenGelenKumas", WhoAmI, barCheckItem1.Checked);
            gridView1.BestFitColumns();
        }

        void StokGirisMusteriden_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["UserId"] = WhoAmI;
        }

        private void GirisCikis(object sender, EventArgs e)
        {
            string ids = gridView1.GetSelectedValues("MusteridenGelenKumasId");
            List<int> mgkIds = new List<int>();
            foreach (int rowHandle in gridView1.GetSelectedRows())
            {
                DataRow dr = gridView1.GetDataRow(rowHandle);
                if (dr["ProductId"] == DBNull.Value)
                    mgkIds.Add(dr["MusteridenGelenKumasId"].ToInt(-1));
            }

            if (mgkIds.Count > 0)
                if (XtraMessageBox.Show("Seçtiğiniz satırlarda artikeli setlenmemiş " + mgkIds.Count + " satır var. Bunları Kod Sihirbazında setlemek ister misiniz?", "DİKKAT!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    KodSihirbazi_Click(sender, e);

            gridView1.Update("", "MusteridenGelenKumas");
            gridView1.RefreshDataSource(false, "InsMusteridenGelenKumas", ids, WhoAmI);
            gridView1.BestFitColumns();
        }

        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            if (
                checkEmpty(gridView1.DataTable(), "Qty") ||
                checkEmpty(gridView1.DataTable(), "BrandId") ||
                checkEmpty(gridView1.DataTable(), "OwnerId")
                )
            {
                XtraMessageBox.Show("Zorunlu alanları doldurduktan sonra tekrar deneyin", "Eksik giriş", MessageBoxButtons.OK);
                return;
            }

            gridView1.Update("", "MusteridenGelenKumas");
            gridView1.RefreshDataSource("GetMusteridenGelenKumas", WhoAmI, barCheckItem1.Checked);
            gridView1.BestFitColumns();
        }
    }
}
